
function saveStorage(key,value){
    chrome.storage.local.set({ key: value }).then(() => {
    });
}
function getStorage(key){
    chrome.storage.local.get(["key"]).then((result) => {
    });
}
function save_options() {
    var checked = $('input:radio[name=bitrate]:checked');
    if(checked.length > 0) {
        var bitrate = checked.attr('value');
        saveStorage("bitrate", bitrate);
    }
    var filename_format = $('#filename_format')[0].value;
    if(filename_format == '') {
        filename_format = '$song';
    }
    saveStorage("filename_format", filename_format);
    // Update status to let user know options were saved.
    $('#status').removeClass('hide');
    setTimeout(function() {
        $('#status').addClass('hide');
    }, 750);
}

// Restores select box state to saved value from localStorage.
function restore_options() {
    var bitrate = getStorage["bitrate"];
    if (!bitrate) {
        return;
    }
    var radios = $('input:radio[name=bitrate]');
    radios.filter('[value=' + bitrate + ']').prop('checked', true);

    var filename_format = getStorage["filename_format"];
    if(filename_format) {
        $('#filename_format')[0].value = filename_format;
    }
}

jQuery(document).ready(function ($) {
    restore_options();
    $('#save').click(save_options);
})
//document.addEventListener('DOMContentLoaded', restore_options);
//document.querySelector('#save').addEventListener('click', save_options);
